package com.example.springboottestgit.controller;

import com.example.springboottestgit.bean.CommonException;
import com.example.springboottestgit.bean.ResultMsg;
import com.example.springboottestgit.bean.vo.*;
import com.example.springboottestgit.service.FileService;
import com.example.springboottestgit.service.InviteTenderService;
import com.example.springboottestgit.service.SubmitTenderService;
import com.example.springboottestgit.service.TenderBsnServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:智能招投标识别匹配系统
 * @author: ZZZ
 * @time: 2021/4/12 21:08
 */

@RestController
@RequestMapping("/tender")
public class TenderController {
    @Autowired
    private InviteTenderService inviteTenderService;
    @Autowired
    private SubmitTenderService submitTenderService;
    @Autowired
    private FileService fileService;

    @Autowired
    private TenderBsnServer tenderBsnServer;

    @GetMapping("/bsn")
    public ResultMsg<Object> getBsn() {
        Map<String, String> map = new HashMap<>();
        map.put("bsn", tenderBsnServer.getBsn());
        return ResultMsg.success(map);
    }


    @PostMapping("/invite/tender/upload")
    public ResultMsg<Object> inviteTenderUpload(@RequestParam("file") MultipartFile multipartFile, @RequestParam("bsn") String bsn, @RequestParam("fileType") int fileType) {

        String fileName = multipartFile.getOriginalFilename();
        InviteTenderFileVO inviteTenderFileVO = null;

        if (fileName.endsWith(".pdf")) {
            inviteTenderFileVO = fileService.inviteTenderUpload(multipartFile, bsn, fileType);
        } else {
            //throw new CommonException("001001", "文件格式错误,请上传 doc、docx、pdf 文件");
            throw new CommonException("001001", "文件格式错误,请上传 pdf 文件格式");
        }

        return ResultMsg.success(inviteTenderFileVO);
    }

    @GetMapping("/invite/tender/detail/query")
    public ResultMsg<Object> inviteTenderDetailQuery(long id) {
        InviteTenderFileVO vo = inviteTenderService.inviteTenderDetailQuery(id);
        return ResultMsg.success(vo);
    }


    @PostMapping("/submit/tender/upload")
    public ResultMsg<Object> submitTenderUpload(@RequestParam("file") MultipartFile multipartFile, @RequestParam("bsn") String bsn, @RequestParam("fileType") int fileType) {
        String fileName = multipartFile.getOriginalFilename();
        SubmitTenderFileVO submitTenderFileVO = null;

        if (!StringUtils.hasText(bsn)) {
            throw new CommonException("001001", "业务流水号错误");
        }

        if (fileName.endsWith(".pdf")) {
            submitTenderFileVO = fileService.submitTenderUpload(multipartFile, bsn, fileType);
        } else {
            //throw new CommonException("001001", "文件格式错误,请上传 doc、docx、pdf 文件");
            throw new CommonException("001001", "文件格式错误,请上传 pdf 文件格式");
        }


        return ResultMsg.success(submitTenderFileVO);
    }

    @GetMapping("/generate")
    public ResultMsg<Object> tenderGenerate(String bsn) {
        submitTenderService.tenderContentQa(bsn);
        return ResultMsg.success();
    }


    @GetMapping("/submit/tender/detail/query")
    public ResultMsg<Object> submitTenderDetailQuery(String bsn, String keywords, Integer inspectItemType, String status) {
        if (!StringUtils.hasText(bsn)) {
            throw new CommonException("001001", "业务流水号错误");
        }
        TenderAnalysisResultVO vo = submitTenderService.submitTenderDetailQuery(bsn, keywords, inspectItemType, status);
        return ResultMsg.success(vo);
    }

    @GetMapping("/submit/tender/view/detail")
    public ResultMsg<Object> submitTenderViewDetail(String bsn) {
        TenderViewDetailVO vo = submitTenderService.submitTenderViewDetail(bsn);
        return ResultMsg.success(vo);
    }


//    @GetMapping("/submit/tender/inspect/list")
//    public ResultMsg<Object> submitTenderInspectList(long id) {
//        List<SubmitTenderInspectVO> list = submitTenderService.submitTenderInspectList(id);
//        return ResultMsg.success(list);
//    }

    @GetMapping("/submit/tender/file/list")
    public ResultMsg<Object> submitTenderFileList(long id) {
        List<SubmitTenderFileVO> list = submitTenderService.submitTenderFileList(id);
        return ResultMsg.success(list);
    }


    @GetMapping("/submit/tender/query")
    public ResultMsg<List<FileVO>> submitTenderQuery(long id) {
        //List<FileVO> list = submitTenderService.submitTenderQuery(id);
        return ResultMsg.success();
    }

    @GetMapping("/submit/tender/delete")
    public ResultMsg<Object> submitTenderDelete(long id, long assId) {
        //submitTenderService.submitTenderDelete(id, assId);
        return ResultMsg.success();
    }

}

